﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using RuleCode.Table;

namespace map.member
{
    public partial class Items : System.Web.UI.Page
    {
        #region 页面载入
        protected void Page_Load(object sender, EventArgs e)
        {               
            if (!IsPostBack)
            {
                BindMainWebPager(false );
            }
        }
        #endregion


        #region 数据绑定相关
        /// <summary>
        /// MainWebPager 当前页面 改变事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void MainWebPager_PageIndexChanged(object sender, RuleCode.Controls.PageChangedEventArgs e)
        {
            this.MainWebPager.CurrentPageIndex = e.NewPageIndex;
            BindMainWebPager(false );
        }

        /// <summary>
        /// 绑定数据源
        /// </summary>
        protected void BindMainWebPager(bool useSearchKeys)
        {
            bool hasData = false;
            if (AliApi.appUserId != string.Empty)
            {
                string filter = "taobaouserid='"+AliApi.appUserId +"'";
                if (useSearchKeys)
                {
                    if(this.SearchKeyItemTitleTextBox .Text .Trim()!=string.Empty )
                    {
                        filter += " and taobaotitle like '%"+this.SearchKeyItemTitleTextBox.Text  + "%'";
                    }
                    if (this.SearchKeyItemStatusDropDownList.SelectedValue != string.Empty )
                    {
                        filter += " and taobaostuff_status='"+this.SearchKeyItemStatusDropDownList .SelectedValue +"'";
                    }
                }

                taobaouseritems useritems = taobaouseritems.Find(filter);
                this.MainWebPager.DataSource = useritems.MyTable;
                this.MainWebPager.DataBind();
                hasData = (useritems.Count > 0);
            }
            this.NoDataDiv.Visible = !hasData;
        }

        /// <summary>
        /// 绑定数据源
        /// </summary>
        protected void BindMainWebPager(taobaouseritems useritems)
        {
            if (useritems != null)
            {
                bool hasData = (useritems.Count >0);
                this.MainWebPager.DataSource = useritems.MyTable;
                this.MainWebPager.DataBind();
                this.NoDataDiv.Visible = !hasData;
            }
        }
        #endregion

        #region 控件的相关事件
        protected void selectAllItemCheck0_ServerCheckedChanged(object sender, EventArgs e)
        {
            foreach (DataListItem item in this.MainDataList.Items)
            {
                CheckBox itemCheckBox = item.FindControl("taobaoitemcheckbox") as CheckBox;
                itemCheckBox.Checked = this.selectAllItemCheck0 .Checked;
            }
            this.selectAllItemCheck1.Checked = this.selectAllItemCheck0.Checked;
            this.selectOthersItemCheck.Checked = false;
        }

        protected void selectAllItemCheck1_ServerCheckedChanged(object sender, EventArgs e)
        {
            foreach (DataListItem item in this.MainDataList.Items)
            {
                CheckBox itemCheckBox = item.FindControl("taobaoitemcheckbox") as CheckBox;
                itemCheckBox.Checked = this.selectAllItemCheck1 .Checked;
            }
            this.selectAllItemCheck0.Checked = this.selectAllItemCheck1.Checked;
            this.selectOthersItemCheck.Checked = false;
        }

        protected void selectOthersItemCheck_ServerCheckedChanged(object sender, EventArgs e)
        {
            foreach (DataListItem item in this.MainDataList.Items)
            {
                CheckBox itemCheckBox = item.FindControl("taobaoitemcheckbox") as CheckBox;
                itemCheckBox.Checked = !itemCheckBox.Checked;
            }
            this.selectAllItemCheck1.Checked = false;
            this.selectAllItemCheck0.Checked = false;
        }

        protected void DeleteButton_Click(object sender, EventArgs e)
        {
            if (AliApi.appUserId != string.Empty)
            {
                string filter = "taobaouserid='" + AliApi.appUserId + "'";
                taobaouseritems useritems = taobaouseritems.Find(filter);

                for (int i = 0; i < this.MainDataList.Items.Count; i++)
                {
                    CheckBox itemcheckbox = this.MainDataList.Items[i].FindControl("taobaoitemcheckbox") as CheckBox ;
                    if (itemcheckbox.Checked)
                    {
                        string recid = this.MainDataList.DataKeys[i].ToString ();
                        useritems.Delete("RECID="+recid );
                    }
                }
                BindMainWebPager(useritems);
            }
        }

        protected void searchItemButton_ServerClick(object sender, EventArgs e)
        {
            if (AliApi.appUserId != string.Empty)
            {
                BindMainWebPager(true);
            }
            ScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(), "initsearchFilter", "<script type=\"text/javascript\">initsearchFilter();</script>", false);
        }
        #endregion

        public string GetItemUrl(string itemRecid)
        {
            return ResolveUrl("~/member/i.aspx?id="  + itemRecid);
        }
    }
}
